package com.ccdc.lbcmsystem.controller;

import com.ccdc.lbcmsystem.mapper.*;
import com.ccdc.lbcmsystem.pojo.Elderly;
import com.ccdc.lbcmsystem.pojo.Gift;
import com.ccdc.lbcmsystem.pojo.Unit;
import com.ccdc.lbcmsystem.util.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class GiftController {

    @Autowired
    GiftMapper giftMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ExamineMapper examineMapper;

    @Autowired
    ChildrenMapper childrenMapper;

    @Autowired
    ElderlyMapper elderlyMapper;

    @Autowired
    VeteranMapper veteranMapper;

    @Autowired
    UnitMapper unitMapper;



    @RequestMapping("/giftMoney")
    public String giftMoney(Model model){
        int money4 = 0;
        List<Gift> gifts = giftMapper.queryCheckedGiftList();
        for (Gift gift : gifts) {
            Integer money = gift.getMoney();
            money4+=money;
        }
        model.addAttribute("totalMoney",money4);
        int money1 = 0;
        List<Gift> childrenGiftList = giftMapper.queryChildrenCheckedGiftList();
        for (Gift gift : childrenGiftList) {
            Integer childrenMoney = gift.getMoney();
            money1+=childrenMoney;
        }
        int money2 = 0;
        List<Gift> elderlyGiftList = giftMapper.queryElderlyCheckedGiftList();
        for (Gift gift : elderlyGiftList) {
            Integer elderlyMoney = gift.getMoney();
            money2+=elderlyMoney;
        }
        int money3 = 0;
        List<Gift> veteranGiftList = giftMapper.queryVeteranCheckedGiftList();
        for (Gift gift : veteranGiftList) {
            Integer veteranMoney = gift.getMoney();
            money3+=veteranMoney;
        }
        System.out.println(money1);
        System.out.println(money2);
        System.out.println(money3);
        unitMapper.updateUnit("村民委员会",elderlyGiftList.size(),money2);
        unitMapper.updateUnit("军人办公室",veteranGiftList.size(),money3);
        unitMapper.updateUnit("妇女联合会",childrenGiftList.size(),money1);
        List<Unit> unitLists = unitMapper.getUnitList();
        model.addAttribute("unitLists",unitLists);
        return "gift/giftMoney";
    }
    @RequestMapping("/detailed/{id}")
    public String detailed(@PathVariable("id") Integer id,Model model){
        if (id == 1){
            List<Gift> gifts = giftMapper.queryChildrenCheckedGiftList();
            model.addAttribute("gifts",gifts);
            return "childrenGift/childrenGiftDetailedList";
        }
       if(id == 2){
           List<Gift> gifts = giftMapper.queryElderlyCheckedGiftList();
           model.addAttribute("gifts",gifts);
           return "elderlyGift/elderlyGiftDetailedList";
       }if (id == 3){
           List<Gift> gifts = giftMapper.queryVeteranCheckedGiftList();
           model.addAttribute("gifts",gifts);
           return "veteranGift/veteranGiftDetailedList";
       }else {
           return "gift/giftList";
       }

    }

    @RequestMapping("/giftListDerive")
    public void giftListDerive(HttpServletResponse response){
        List<Gift> unitList = giftMapper.queryCheckedGiftList();

        ExcelUtil.exportExcel(unitList,"礼品汇总信息","sheet1",Gift.class,"礼品列表.xls",response);
    }


    @RequestMapping("/GiftDerive")
    public void derive(HttpServletResponse response){
        List<Unit> unitList = unitMapper.getUnitList();

        ExcelUtil.exportExcel(unitList,"礼品总金额汇总信息","sheet1",Unit.class,"礼品总金额.xls",response);
    }

    @RequestMapping("/childrenGiftDerive")
    public void childrenGiftDerive(HttpServletResponse response){
        List<Gift> gifts = giftMapper.queryChildrenCheckedGiftList();
        ExcelUtil.exportExcel(gifts,"妇联礼品汇总信息","sheet1",Gift.class,"妇联礼品详细列表.xls",response);
    }

    @RequestMapping("/elderlyGiftDerive")
    public void elderlyGiftDerive(HttpServletResponse response){
        List<Gift> gifts = giftMapper.queryElderlyCheckedGiftList();
        ExcelUtil.exportExcel(gifts,"村委会礼品汇总信息","sheet1",Gift.class,"村委会礼品详细列表.xls",response);
    }
    @RequestMapping("/veteranGiftDerive")
    public void veteranGiftDerive(HttpServletResponse response){
        List<Gift> gifts = giftMapper.queryVeteranCheckedGiftList();
        ExcelUtil.exportExcel(gifts,"军人办礼品汇总信息","sheet1",Gift.class,"军人办礼品详细列表.xls",response);
    }


    @RequestMapping("/giftList")
    public String giftList(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                               @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "gift/giftList";
    }
    @RequestMapping("/childrenGiftList")
    public String childrenGiftList(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                           @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryChildrenGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "childrenGift/childrenGiftList";
    }
    @RequestMapping("/elderlyGiftList")
    public String elderlyGiftList(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                   @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryElderlyGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "elderlyGift/elderlyGiftList";
    }
    @RequestMapping("/veteranGiftList")
    public String veteranGiftList(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                  @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryVeteranGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "veteranGift/veteranGiftList";
    }

    @RequestMapping("/searchGift")
    public String searchGift(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryListGiftByName(name);
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        session.setAttribute("name",name);
        model.addAttribute("childrensPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "gift/giftSearchList";
    }

    @RequestMapping("/searchElderlyGift")
    public String searchElderlyGift(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryListElderlyGiftByName(name);
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        session.setAttribute("name",name);
        model.addAttribute("childrensPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "elderlyGift/elderlyGiftSearchList";
    }

    @RequestMapping("/searchChildrenGift")
    public String searchChildrenGift(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                             @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryListChildrenGiftByName(name);
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        session.setAttribute("name",name);
        model.addAttribute("childrensPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "childrenGift/childrenGiftSearchList";
    }

    @RequestMapping("/searchVeteranGift")
    public String searchVeteranGift(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                     @RequestParam(defaultValue = "10") Integer pageSize, String name, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryListVeteranGiftByName(name);
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        session.setAttribute("name",name);
        model.addAttribute("childrensPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "veteranGift/veteranGiftSearchList";
    }


    @RequestMapping("/toChildrenGiftAdd")
    public String toChildrenGiftAdd(Model model){

        int size = childrenMapper.queryChildrenList().size();
        model.addAttribute("childrenNum",size);
        return "childrenGift/childrenGiftAdd";
    }
    @RequestMapping("/childrenGiftAdd")
    @ResponseBody
    public String childrenGiftAdd(Gift gift) {

        String msg = "";
        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = new Date();
        String time = sft.format(createTime);
        gift.setCreateTime(time);
        gift.setProcessStatus(0);
        gift.setDeclaringUnit(1);
        System.out.println("申报单位"+gift.getDeclaringUnit());
        try {
            giftMapper.addChildrenGift(gift);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "新增失败";
            return msg;
        }

    }




    @RequestMapping("/toElderlyGiftAdd")
    public String toElderlyGiftAdd(Model model){
        int size = elderlyMapper.queryElderlyList().size();
        model.addAttribute("elderlyNum",size);
        return "elderlyGift/elderlyGiftAdd";
    }
    @RequestMapping("/elderlyGiftAdd")
    @ResponseBody
    public String elderlyGiftAdd(Gift gift) {
        String msg = "";
        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = new Date();
        String time = sft.format(createTime);
        gift.setCreateTime(time);
        gift.setProcessStatus(0);
        gift.setDeclaringUnit(3);
        System.out.println("申报单位"+gift.getDeclaringUnit());
        try {
            giftMapper.addChildrenGift(gift);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "新增失败";
            return msg;
        }

    }

    @RequestMapping("/toVeteranGiftAdd")
    public String toVeteranGiftAdd(Model model){

        int size = veteranMapper.queryVeteranList().size();
        model.addAttribute("childrenNum",size);
        return "veteranGift/veteranGiftAdd";
    }
    @RequestMapping("/veteranGiftAdd")
    @ResponseBody
    public String veteranGiftAdd(Gift gift) {

        String msg = "";
        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = new Date();
        String time = sft.format(createTime);
        gift.setCreateTime(time);
        gift.setProcessStatus(0);
        gift.setDeclaringUnit(2);
        System.out.println("申报单位"+gift.getDeclaringUnit());
        try {
            giftMapper.addChildrenGift(gift);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "新增失败";
            return msg;
        }

    }

//    @RequestMapping("/toGiftAdd")
//    public String toGiftAdd(){
//        return "gift/giftAdd";
//    }
//    @RequestMapping("/giftAdd")
//    @ResponseBody
//    public String giftAdd(Gift gift) {
//        String msg = "";
//        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date createTime = new Date();
//        String time = sft.format(createTime);
//        gift.setCreateTime(time);
//        gift.setProcessStatus(0);
//        try {
//            giftMapper.addGift(gift);
//            msg = "OK";
//            return msg;
//        }catch (Exception e){
//            msg = "新增失败";
//            return msg;
//        }
//
//    }

    @RequestMapping("/giftLookInfo/{id}")
    public String giftLookInfo(@PathVariable("id") Integer id, Model model){
        Gift gift = giftMapper.queryGiftById(id);
        model.addAttribute("gift",gift);
        return "gift/giftLookInfo";
    }

    @RequestMapping("/childrenGiftLookInfo/{id}")
    public String childrenGiftLookInfo(@PathVariable("id") Integer id, Model model){
        Gift gift = giftMapper.queryGiftById(id);
        model.addAttribute("gift",gift);
        return "childrenGift/childrenGiftLookInfo";
    }

    @RequestMapping("/elderlyGiftLookInfo/{id}")
    public String elderlyGiftLookInfo(@PathVariable("id") Integer id, Model model){
        Gift gift = giftMapper.queryGiftById(id);
        model.addAttribute("gift",gift);
        return "elderlyGift/elderlyGiftLookInfo";
    }

    @RequestMapping("/veteranGiftLookInfo/{id}")
    public String veteranGiftLookInfo(@PathVariable("id") Integer id, Model model){
        Gift gift = giftMapper.queryGiftById(id);
        model.addAttribute("gift",gift);
        return "veteranGift/veteranGiftLookInfo";
    }

    @RequestMapping("/giftDelete/{id}")
    public String giftDel(@PathVariable("id") Integer id){
        System.out.println(id);
        System.out.println(giftMapper.delGiftById(id));
        return "redirect:/giftList";
    }
    @RequestMapping("/elderlyGiftDelete/{id}")
    public String elderlyGiftDelete(@PathVariable("id") Integer id){
        System.out.println(id);
        System.out.println(giftMapper.delGiftById(id));
        return "redirect:/elderlyGiftList";
    }

    @RequestMapping("/childrenGiftDelete/{id}")
    public String childrenGiftDel(@PathVariable("id") Integer id){
        System.out.println(id);
        System.out.println(giftMapper.delGiftById(id));
        return "redirect:/childrenGiftList";
    }

    @RequestMapping("/veteranGiftDelete/{id}")
    public String veteranGiftDelete(@PathVariable("id") Integer id){
        System.out.println(id);
        System.out.println(giftMapper.delGiftById(id));
        return "redirect:/veteranGiftList";
    }

    @RequestMapping("/toGiftChecked")
    public String toGiftChecked(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryCheckedGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);


        return "gift/giftChecked";

    }

    @RequestMapping("/toChildrenGiftChecked")
    public String toChildrenGiftChecked(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryChildrenCheckedGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "childrenGift/childrenGiftChecked";

    }

    @RequestMapping("/toElderlyGiftChecked")
    public String toElderlyGiftChecked(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                        @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryElderlyCheckedGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "elderlyGift/elderlyGiftChecked";

    }

    @RequestMapping("/toVeteranGiftChecked")
    public String toVeteranGiftChecked(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                       @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryVeteranCheckedGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "veteranGift/veteranGiftChecked";

    }

    @RequestMapping("/toChildrenGiftUnChecked")
    public String toChildrenGiftUnChecked(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                        @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryChildrenUnCheckedGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "childrenGift/childrenGiftUnChecked";

    }

    @RequestMapping("/toElderlyGiftUnChecked")
    public String toElderlyGiftUnChecked(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                          @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryElderlyUnCheckedGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "elderlyGift/elderlyGiftUnChecked";

    }

    @RequestMapping("/toVeteranGiftUnChecked")
    public String toVeteranGiftUnChecked(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                         @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryVeteranUnCheckedGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);
        return "veteranGift/veteranGiftUnChecked";

    }


    @RequestMapping("/toGiftUnChecked")
    public String toGiftUnChecked(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                  @RequestParam(defaultValue = "10") Integer pageSize){

        PageHelper.startPage(startPage, pageSize);
        List<Gift> gifts = giftMapper.queryUnCheckedGiftList();
        PageInfo<Gift> giftPageInfo = new PageInfo<>(gifts);
        model.addAttribute("giftPageInfo", giftPageInfo);
        model.addAttribute("gifts",gifts);

        return "gift/giftUnChecked";

    }


}

